package com.qax.task.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.nio.charset.Charset;

public class JsonKit {

    private static ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    public static byte[] serializeObject2bytes(Object o) throws IOException {
        return mapper.writeValueAsString(o).getBytes(Charset.defaultCharset());
    }


    public static String serializeObject2Chars(Object o) throws IOException {
        return mapper.writeValueAsString(o);
    }


    public static <T> T deserializeByteArray(byte[] s, Class<T> typeReference)
            throws IOException {
        return mapper.readValue(s, typeReference);
    }

}
